package com.sino.operation.controller;

import com.sino.operation.commons.response.CenterResponseEntity;
import com.sino.operation.service.basic.RedisTaskUserService;
import com.sino.operation.vo.TaskGetParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Title: TaskController
 * @Description: 前端任务的暂停与继续获取
 * @Author dengls
 * @date 2021/10/22 15:49
 */
@Slf4j
@RestController
@RequestMapping("task")
@Api(value = "任务", tags = "暂停、继续获取")
public class TaskController {

    @Autowired
    private RedisTaskUserService redisTaskUserService;

    @PostMapping("changeStatus")
    @ApiOperation(value = "暂停/恢复接受任务", notes = "暂停/恢复接受任务")
    public CenterResponseEntity changeStatus(@RequestBody @Valid TaskGetParam taskGetParam) {
        log.info("暂停/恢复接受任务开始-changeStatus,参数：" + taskGetParam);
        //判断状态
        try {
            if (taskGetParam.getIsStop()) {
                //暂停获取，移除Redis中用户信息
                redisTaskUserService.removeUserData(taskGetParam.getPostNo(), taskGetParam.getCenterTellerNo());
            } else {
                //继续获取，添加用户进Redis中
                redisTaskUserService.addUserData(taskGetParam.getPostNo(), taskGetParam.getCenterTellerNo());
            }
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return CenterResponseEntity.buildError(Boolean.class).setData(false).setMsg(e.getMessage());
        }
        log.info("暂停/恢复接受任务结束-changeStatus");
        return CenterResponseEntity.build(Boolean.class).setData(true);
    }
}
